using System;
using System.Net;
using System.Windows;
using System.Xml;
using ShellLight.Contract;
using System.Xml.Linq;

namespace ShellLight.Framework.Commands
{
    public class HttpPostCommand : UICommand
    {

        //POST /feeds/content/domainName/siteName HTTP/1.1
        //Host: sites.google.com
        //GData-Version: 1.1
        //Authorization: <your authorization header here>
        //Content-Length: 328
        //Content-Type: application/atom+xml

        //<entry xmlns="http://www.w3.org/2005/Atom">
        //  <category scheme="http://schemas.google.com/g/2005#kind"
        //      term="http://schemas.google.com/sites/2008#webpage" label="webpage"/>
        //  <title>New Webpage Title</title>
        //  <content type="xhtml">
        //    <div xmlns="http://www.w3.org/1999/xhtml">HTML body goes here</div>
        //  </content>
        //</entry>

        public override UIElement DoShow()
        {

            var webClient = new WebClient();
            webClient.Headers[HttpRequestHeader.ContentType] = "application/atom+xml";
           
            webClient.OpenWriteCompleted += (s, e) =>
                                                {
                                                    if (!e.Cancelled && (e.Error == null))
                                                    {
                                                        var xml = new XElement("entry",
                                                                     new XElement("category"),
                                                                     new XAttribute("scheme", "http://schemas.google.com/g/2005#kind"),
                                                                     new XAttribute("term", "http://schemas.google.com/sites/2008#webpage"),
                                                                     new XAttribute("label", "webpage"),
                                                            new XElement("title", "New Webpage Title"));

                                                        var xmlWriter = XmlWriter.Create(e.Result);
                                                        xml.WriteTo(xmlWriter);
                                                        xmlWriter.Close();
                                                        e.Result.Close();
                                                    }
                                                    ;
                                                };

            webClient.OpenWriteAsync(new Uri("http://sites.google.com/feeds/content/shelllight/site", UriKind.Absolute), "POST");

            return null;
        }
    }
}